home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / 16 windows forms / otherobjects / applicationform.vb < prev    next >
Encoding:
Text File  |  2002-03-16  |  8.7 KB  |  205 lines

  1. Public Class ApplicationForm
  2.     Inherits System.Windows.Forms.Form
  3.  
  4. #Region " Windows Form Designer generated code "
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.  
  9.         'This call is required by the Windows Form Designer.
  10.         InitializeComponent()
  11.  
  12.         'Add any initialization after the InitializeComponent() call
  13.         AddHandler Application.Idle, AddressOf Application_Idle
  14.         AddHandler Application.ApplicationExit, AddressOf Application_ApplicationExit
  15.     End Sub
  16.  
  17.     'Form overrides dispose to clean up the component list.
  18.     Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
  19.  
  20.         RemoveHandler Application.Idle, AddressOf Application_Idle
  21.         RemoveHandler Application.ApplicationExit, AddressOf Application_ApplicationExit
  22.  
  23.         If disposing Then
  24.             If Not (components Is Nothing) Then
  25.                 components.Dispose()
  26.             End If
  27.         End If
  28.         MyBase.Dispose(disposing)
  29.     End Sub
  30.     Friend WithEvents btnShowProperties As System.Windows.Forms.Button
  31.     Friend WithEvents txtResult As System.Windows.Forms.TextBox
  32.     Friend WithEvents chkShowIdle As System.Windows.Forms.CheckBox
  33.     Friend WithEvents ProgressBar1 As System.Windows.Forms.ProgressBar
  34.     Friend WithEvents btnEnd As System.Windows.Forms.Button
  35.     Friend WithEvents btnThrow As System.Windows.Forms.Button
  36.  
  37.     'Required by the Windows Form Designer
  38.     Private components As System.ComponentModel.Container
  39.  
  40.     'NOTE: The following procedure is required by the Windows Form Designer
  41.     'It can be modified using the Windows Form Designer.  
  42.     'Do not modify it using the code editor.
  43.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  44.         Me.btnShowProperties = New System.Windows.Forms.Button()
  45.         Me.txtResult = New System.Windows.Forms.TextBox()
  46.         Me.btnEnd = New System.Windows.Forms.Button()
  47.         Me.chkShowIdle = New System.Windows.Forms.CheckBox()
  48.         Me.ProgressBar1 = New System.Windows.Forms.ProgressBar()
  49.         Me.btnThrow = New System.Windows.Forms.Button()
  50.         Me.SuspendLayout()
  51.         '
  52.         'btnShowProperties
  53.         '
  54.         Me.btnShowProperties.Location = New System.Drawing.Point(16, 16)
  55.         Me.btnShowProperties.Name = "btnShowProperties"
  56.         Me.btnShowProperties.Size = New System.Drawing.Size(112, 40)
  57.         Me.btnShowProperties.TabIndex = 0
  58.         Me.btnShowProperties.Text = "Show Properties"
  59.         '
  60.         'txtResult
  61.         '
  62.         Me.txtResult.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
  63.                     Or System.Windows.Forms.AnchorStyles.Left) _
  64.                     Or System.Windows.Forms.AnchorStyles.Right)
  65.         Me.txtResult.Location = New System.Drawing.Point(152, 16)
  66.         Me.txtResult.Multiline = True
  67.         Me.txtResult.Name = "txtResult"
  68.         Me.txtResult.ScrollBars = System.Windows.Forms.ScrollBars.Both
  69.         Me.txtResult.Size = New System.Drawing.Size(440, 240)
  70.         Me.txtResult.TabIndex = 1
  71.         Me.txtResult.Text = ""
  72.         Me.txtResult.WordWrap = False
  73.         '
  74.         'btnEnd
  75.         '
  76.         Me.btnEnd.Location = New System.Drawing.Point(16, 80)
  77.         Me.btnEnd.Name = "btnEnd"
  78.         Me.btnEnd.Size = New System.Drawing.Size(112, 40)
  79.         Me.btnEnd.TabIndex = 0
  80.         Me.btnEnd.Text = "End the application"
  81.         '
  82.         'chkShowIdle
  83.         '
  84.         Me.chkShowIdle.Location = New System.Drawing.Point(8, 272)
  85.         Me.chkShowIdle.Name = "chkShowIdle"
  86.         Me.chkShowIdle.Size = New System.Drawing.Size(304, 16)
  87.         Me.chkShowIdle.TabIndex = 3
  88.         Me.chkShowIdle.Text = "Show Idle Events"
  89.         '
  90.         'ProgressBar1
  91.         '
  92.         Me.ProgressBar1.Dock = System.Windows.Forms.DockStyle.Bottom
  93.         Me.ProgressBar1.Location = New System.Drawing.Point(0, 293)
  94.         Me.ProgressBar1.Name = "ProgressBar1"
  95.         Me.ProgressBar1.Size = New System.Drawing.Size(608, 16)
  96.         Me.ProgressBar1.TabIndex = 4
  97.         '
  98.         'btnThrow
  99.         '
  100.         Me.btnThrow.Location = New System.Drawing.Point(16, 144)
  101.         Me.btnThrow.Name = "btnThrow"
  102.         Me.btnThrow.Size = New System.Drawing.Size(112, 40)
  103.         Me.btnThrow.TabIndex = 0
  104.         Me.btnThrow.Text = "Throw an exception"
  105.         '
  106.         'ApplicationForm
  107.         '
  108.         Me.AutoScaleBaseSize = New System.Drawing.Size(7, 17)
  109.         Me.ClientSize = New System.Drawing.Size(608, 309)
  110.         Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.btnThrow, Me.btnEnd, Me.ProgressBar1, Me.txtResult, Me.btnShowProperties, Me.chkShowIdle})
  111.         Me.Font = New System.Drawing.Font("Microsoft Sans Serif", 11!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
  112.         Me.Name = "ApplicationForm"
  113.         Me.Text = "The Application object"
  114.         Me.ResumeLayout(False)
  115.  
  116.     End Sub
  117.  
  118. #End Region
  119.  
  120.     ' show Application object properties 
  121.  
  122.     Private Sub btnShowProperties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowProperties.Click
  123.         Dim msg As String = "AllowQuit = {1}{0}CommonAppDataPath = {2}{0}" _
  124.             & "CommonPathDataRegistry = {3}{0}CompanyName = {4}{0}" _
  125.             & "CurrentCulture = {5}{0}CurrentInputLanguage = {6}{0}" _
  126.             & "ExecutablePath = {7}{0}LocalUserAppDataPath = {8}{0}" _
  127.             & "ProductName = {9}{0}ProductVersion = {10}{0}" _
  128.             & "SafeTopLevelCaptionFormat = {11}{0}StartupPath = {12}{0}" _
  129.             & "UserAppDataPath = {13}{0}UserAppDataRegistry = {14}{0}"
  130.  
  131.         txtResult.Text = String.Format(msg, ControlChars.CrLf, Application.AllowQuit, Application.CommonAppDataPath, _
  132.             Application.CommonAppDataRegistry, Application.CompanyName, _
  133.             Application.CurrentCulture, Application.CurrentInputLanguage.Culture, _
  134.             Application.ExecutablePath, Application.LocalUserAppDataPath, _
  135.             Application.ProductName, Application.ProductVersion, _
  136.             Application.SafeTopLevelCaptionFormat, Application.StartupPath, _
  137.             Application.UserAppDataPath, Application.UserAppDataRegistry)
  138.     End Sub
  139.  
  140.     ' exit the application
  141.  
  142.     Private Sub btnEnd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnd.Click
  143.         Application.Exit()
  144.     End Sub
  145.  
  146.     ' simulate an exception, that is then trapped 
  147.  
  148.     Private Sub btnThrow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThrow.Click
  149.         Throw New ArgumentException("The parameter was invalid")
  150.     End Sub
  151.  
  152.     ' this event fires when the UI becomes idle again
  153.  
  154.     Private Sub Application_Idle(ByVal sender As System.Object, ByVal e As System.EventArgs)
  155.         If chkShowIdle.Checked Then
  156.             ProgressBar1.Value = (ProgressBar1.Value Mod ProgressBar1.Maximum) + 1
  157.         End If
  158.     End Sub
  159.  
  160.     ' this event fires when the application is exiting
  161.  
  162.     Private Sub Application_ApplicationExit(ByVal sender As System.Object, ByVal e As System.EventArgs)
  163.         MessageBox.Show("The application is about to exit", "ApplicationExit event", MessageBoxButtons.OK, MessageBoxIcon.Information)
  164.     End Sub
  165. End Class
  166.  
  167. ' To test the ThreadException event and global error handlers, 
  168. ' Sub Main should be the Startup object.
  169.  
  170. Module MainModule
  171.  
  172.     <STAThread()> _
  173.     Sub Main()
  174.         ' Add the event handler to to the event.
  175.         AddHandler Application.ThreadException, AddressOf Application_ThreadException
  176.         ' Run the application.
  177.         Application.Run(New StartupForm())
  178.     End Sub
  179.  
  180.     ' this event fires when an unhandled exception is thrown
  181.  
  182.     Sub Application_ThreadException(ByVal sender As Object, ByVal e As System.Threading.ThreadExceptionEventArgs)
  183.         Try
  184.             ' Display an informative message.
  185.             Dim msg As String = String.Format("An error has occurred:{0}{0}{1}{0}{0}{2}", _
  186.                 ControlChars.Cr, e.Exception.Message, e.Exception.StackTrace)
  187.             Dim result As DialogResult = _
  188.                 MessageBox.Show(msg, "Application Error", MessageBoxButtons.AbortRetryIgnore, MessageBoxIcon.Error)
  189.             If result = DialogResult.Abort Then
  190.                 Application.Exit()
  191.             End If
  192.  
  193.         Catch
  194.             ' if the above message box couldn't be displayed because the Exception object
  195.             ' wasn't available, try with a simpler msgbox, and then close the application anyway
  196.             ' 
  197.             Try
  198.                 MessageBox.Show("The application will be terminated", "Fatal Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
  199.             Finally
  200.                 Application.Exit()
  201.             End Try
  202.         End Try
  203.     End Sub
  204.  
  205. End Module